home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Applications / MacPerl 5.1.3 / Mac_Perl_513_src / MacPerl5 / MPWindow.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-08-18  |  30.3 KB  |  1,261 lines  |  [TEXT/MPS ]

  1. /*********************************************************************
  2. Project    :    MacPerl            -    Real Perl Application
  3. File        :    MPWindow.c        -
  4. Author    :    Matthias Neeracher
  5.  
  6. A lot of this code is borrowed from 7Edit written by
  7. Apple Developer Support UK
  8.  
  9. Language    :    MPW C
  10.  
  11. $Log: MPWindow.c,v $
  12. Revision 1.2  1994/05/04  02:52:40  neeri
  13. Inline Input.
  14.  
  15. Revision 1.1  1994/02/27  23:02:22  neeri
  16. Initial revision
  17.  
  18. Revision 0.4  1993/08/17  00:00:00  neeri
  19. A little more defensiveness
  20.  
  21. Revision 0.3  1993/08/06  00:00:00  neeri
  22. Draw pretty icons
  23.  
  24. Revision 0.2  1993/05/30  00:00:00  neeri
  25. Support Console Windows
  26.  
  27. Revision 0.1  1993/05/29  00:00:00  neeri
  28. Compiles correctly
  29.  
  30. *********************************************************************/
  31.  
  32. #include <Resources.h>
  33. #include <Scrap.h>
  34. #include <Packages.h>
  35. #include <PLStringFuncs.h>
  36. #include <Icons.h>
  37. #include "MPWindow.h"
  38. #include "MPConsole.h"
  39. #include "MPEditions.h"
  40.  
  41. #define    kControlInvisible    0
  42. #define    kControlVisible      0xFF
  43. #define    kScrollbarWidth        16
  44. #define    kScrollbarAdjust        (kScrollbarWidth - 1)
  45. #define    kScrollTweek           2
  46. #define    kTextOffset                5
  47. #define    kButtonScroll          10
  48.  
  49. #define    kMaxPages              1000 /* Assumes pages > 32 pixels high */
  50.  
  51. #define    kHOffset                        20   /* Stagger window offsets */
  52. #define    kVOffset                        20
  53.  
  54. #define    kTBarHeight                20
  55. #define    kMBarHeight                20
  56.  
  57. typedef short PageEndsArray[kMaxPages];
  58.  
  59. #if !defined(powerc) && !defined(__powerc)
  60. #pragma segment Window
  61. #endif
  62.  
  63. pascal DPtr DPtrFromWindowPtr(WindowPtr w)
  64. {
  65.     if (w && (GetWindowKind(w) == PerlWindowKind))
  66.         return((DPtr)GetWRefCon(w));
  67.     else
  68.         return(nil);
  69. } /* DPtrFromWindowPtr */
  70.  
  71. #if !defined(powerc) && !defined(__powerc)
  72. #pragma segment Main
  73. #endif
  74.  
  75. /*
  76.   Scroll the TERec around to match up to the potentially updated scrollbar
  77.   values. This is really useful when the window resizes such that the
  78.   scrollbars become inactive and the TERec had been previously scrolled.
  79. */
  80. pascal void AdjustTE(DPtr theDoc)
  81. {
  82.     short    h;
  83.     short    v;
  84.     TEHandle myText;
  85.  
  86.     myText = theDoc->theText;
  87.     h =
  88.         (myText[0]->viewRect.left - myText[0]->destRect.left) -
  89.         GetCtlValue(theDoc->hScrollBar) + kTextOffset;
  90.  
  91.     v =
  92.         (myText[0]->viewRect.top - myText[0]->destRect.top) -
  93.         GetCtlValue(theDoc->vScrollBar) + kTextOffset;
  94.  
  95.     if (h || v) {
  96.         TEScroll(h, v, theDoc->theText);
  97.         DrawPageExtras(theDoc);
  98.     }
  99. }  /* AdjustTE */
  100.  
  101.  
  102. /*Calculate the new control maximum value and current value, whether it is the horizontal or
  103. vertical scrollbar. The vertical max is calculated by comparing the number of lines to the
  104. vertical size of the viewRect. The horizontal max is calculated by comparing the maximum document
  105. width to the width of the viewRect. The current values are set by comparing the offset between
  106. the view and destination rects. If necessary and we canRedraw, have the control be re-drawn by
  107. calling ShowControl.*/
  108.  
  109. /*TEStyleSample-vertical max originally used line by line calculations-lineheight was a
  110. constant value so it was easy to figure out what the range should be and pin the value
  111. within range. Now we need to use max and min values in pixels rather than in nlines*/
  112.  
  113. #if !defined(powerc) && !defined(__powerc)
  114. #pragma segment Main
  115. #endif
  116.  
  117. pascal void AdjustHV(
  118.     Boolean        isVert,
  119.     ControlHandle  control,
  120.     DPtr           theDoc,
  121.     Boolean        canRedraw)
  122. {
  123.     TEHandle    docTE;
  124.     short       value;
  125.     short           max;
  126.     short           oldValue;
  127.     short           oldMax;
  128.     Rect           sizeRect;
  129.     Boolean        inflate;
  130.  
  131.     sizeRect = theDoc->pageSize;
  132.     docTE    = theDoc->theText;
  133.  
  134.     oldValue = GetCtlValue(control);
  135.     oldMax   = GetCtlMax(control);
  136.     inflate  = 
  137.            (docTE[0]->selStart == docTE[0]->teLength) 
  138.         && (docTE[0]->hText[0][docTE[0]->teLength-1] == '\n');
  139.     if (isVert)
  140.         max = 
  141.             (docTE[0]->nLines+inflate)*docTE[0]->lineHeight
  142.          - (docTE[0]->viewRect.bottom - docTE[0]->viewRect.top);
  143.     else
  144.         max = sizeRect.right - (docTE[0]->viewRect.right - docTE[0]->viewRect.left);
  145.  
  146.     max += kTextOffset + kTextOffset; /* Allow over scroll by kTextOffset */
  147.  
  148.     if (max < 0)
  149.         max = 0; /* check for negative values */
  150.  
  151.     SetCtlMax(control, max);
  152.  
  153.     if (isVert)
  154.         value = docTE[0]->viewRect.top - docTE[0]->destRect.top;
  155.     else
  156.         value = docTE[0]->viewRect.left - docTE[0]->destRect.left;
  157.  
  158.     value += kTextOffset;
  159.  
  160.     if (value < 0) {
  161.         TEScroll(isVert ? 0 : value, isVert ? value : 0, docTE);
  162.         DrawPageExtras(theDoc);
  163.  
  164.         value = 0;
  165.     } else if (value > max) {
  166.         TEScroll(isVert ? 0 : value-max, isVert ? value-max : 0, docTE);
  167.         DrawPageExtras(theDoc);
  168.         
  169.         value = max; /* pin the value to within range */
  170.     }
  171.     SetCtlValue(control, value);
  172.     if (canRedraw && ((max != oldMax) || (value != oldValue)))
  173.         ShowControl(control); /* check to see if the control can be re-drawn */
  174. } /* AdjustHV */
  175.  
  176. #if !defined(powerc) && !defined(__powerc)
  177. #pragma segment Main
  178. #endif
  179.  
  180. pascal void AdjustScrollValues(DPtr theDoc, Boolean canRedraw)
  181. {
  182.     AdjustHV(true,  theDoc->vScrollBar, theDoc, canRedraw);
  183.     AdjustHV(false, theDoc->hScrollBar, theDoc, canRedraw);
  184. }        /* AdjustScrollValues */
  185.  
  186. pascal void GetTERect(WindowPtr window, Rect  *teRect)
  187. {
  188.      *teRect = window->portRect;
  189.      (*teRect).bottom -= kScrollbarAdjust; /* and for the scrollbars */
  190.      (*teRect).right  -= kScrollbarAdjust;
  191. }         /* GetTERect */
  192.  
  193. /* Re-calculate the position and size of the viewRect and the scrollbars.
  194.   kScrollTweek compensates for off-by-one requirements of the scrollbars
  195.   to have borders coincide with the growbox. */
  196.  
  197. pascal void AdjustScrollSizes(DPtr theDoc)
  198. {
  199.     Rect    teRect;
  200.     Rect    myPortRect;
  201.  
  202.     GetTERect(theDoc->theWindow, &teRect); /*start with teRect*/
  203.     myPortRect = theDoc->theWindow->portRect;
  204.  
  205.     (*(theDoc->theText))->viewRect = teRect;
  206.  
  207.     MoveControl(theDoc->vScrollBar, myPortRect.right - kScrollbarAdjust, -1);
  208.     SizeControl(
  209.         theDoc->vScrollBar,
  210.         kScrollbarWidth,
  211.         (myPortRect.bottom - myPortRect.top) - (kScrollbarAdjust - kScrollTweek));
  212.  
  213.     MoveControl(theDoc->hScrollBar, 31, myPortRect.bottom - kScrollbarAdjust);
  214.     SizeControl(
  215.         theDoc->hScrollBar,
  216.         (myPortRect.right - myPortRect.left) - (kScrollbarAdjust - kScrollTweek + 32),
  217.         kScrollbarWidth);
  218. }        /* AdjustScrollSizes */
  219.  
  220. #if !defined(powerc) && !defined(__powerc)
  221. #pragma segment Window
  222. #endif
  223.  
  224. /* Turn off the controls by jamming a zero into their contrlVis fields
  225.   (HideControl erases them and we don't want that). If the controls are to
  226.   be resized as well, call the procedure to do that, then call the procedure
  227.   to adjust the maximum and current values. Finally reset the controls
  228.   to be visible if not in background. */
  229.  
  230. pascal void AdjustScrollbars(DPtr theDoc, Boolean  needsResize)
  231. {
  232.     Boolean    background = gInBackground || theDoc->theWindow != gActiveWindow;
  233.     
  234.     (*(theDoc->vScrollBar))->contrlVis = kControlInvisible; /* turn them off */
  235.     (*(theDoc->hScrollBar))->contrlVis = kControlInvisible;
  236.  
  237.     if (needsResize) /* move and size if needed */
  238.         AdjustScrollSizes(theDoc);
  239.  
  240.     AdjustScrollValues(theDoc, !needsResize && !background); /* fool with max and current value */
  241.  
  242.      /* Now, restore visibility in case we never had to ShowControl during adjustment */
  243.  
  244.     if (!background) {
  245.         (*(theDoc->vScrollBar))->contrlVis = kControlVisible; /* turn them on */
  246.         (*(theDoc->hScrollBar))->contrlVis = kControlVisible;
  247.     } else { /* make sure they stay invisible */
  248.         if ((*(theDoc->vScrollBar))->contrlVis)
  249.             HideControl(theDoc->vScrollBar);
  250.         if ((*(theDoc->vScrollBar))->contrlVis)
  251.             HideControl(theDoc->hScrollBar);
  252.     }
  253. }        /* AdjustScrollbars */
  254.  
  255. #if !defined(powerc) && !defined(__powerc)
  256. #pragma segment Window
  257. #endif
  258.  
  259. pascal void GetWinContentRect(WindowPtr theWindow, Rect *r)
  260. {
  261.     *r         = theWindow->portRect;
  262.     r->right  -= kScrollbarAdjust;
  263.     r->bottom -= kScrollbarAdjust;
  264. }  /* GetWinContentRect */
  265.  
  266. #if !defined(powerc) && !defined(__powerc)
  267. #pragma segment Window
  268. #endif
  269.  
  270. pascal void InvalidateDocument(DPtr theDoc)
  271. {
  272.     GrafPtr oldPort;
  273.  
  274.     GetPort(&oldPort);
  275.     SetPort(theDoc->theWindow);
  276.     InvalRect(&theDoc->theWindow->portRect);
  277.     SetPort(oldPort);
  278. }
  279.  
  280. /* Called when the window has been resized to fix up the controls and content */
  281.  
  282. pascal void ResizeWindow(DPtr theDoc)
  283. {
  284.     AdjustScrollbars(theDoc, true);
  285.     AdjustTE(theDoc);
  286.     InvalidateDocument(theDoc);
  287. }         /* ResizeWindow */
  288.  
  289. /* Called when the window has been resized to fix up the controls and content */
  290.  
  291. pascal void ResizePageSetupForDocument(DPtr theDoc)
  292. {
  293.     theDoc->pageSize = (*(theDoc->thePrintSetup))->prInfo.rPage;
  294.  
  295.     OffsetRect(&(theDoc->pageSize), -theDoc->pageSize.left, -theDoc->pageSize.top);
  296.  
  297.     (*(theDoc->theText))->destRect.right = (*(theDoc->theText))->destRect.left +
  298.                                                         theDoc->pageSize.right;
  299.  
  300.     TECalText(theDoc->theText);
  301.  
  302.     ResizeWindow(theDoc);
  303. }         /* ResizePageSetupForDocument */
  304.  
  305. #if !defined(powerc) && !defined(__powerc)
  306. #pragma segment Main
  307. #endif
  308.  
  309. /* Common algorithm for setting the new value of a control. It returns the actual amount
  310. the value of the control changed. Note the pinning is done for the sake of returning
  311. the amount the control value changed. */
  312.  
  313. pascal void CommonAction(ControlHandle control, short *amount)
  314. {
  315.     short   value;
  316.     short   max;
  317.  
  318.     value   = GetCtlValue(control); /* get current value */
  319.     max     = GetCtlMax(control); /* and max value */
  320.     *amount = value - *amount;
  321.     if (*amount < 0)
  322.           *amount = 0;
  323.     else if (*amount > max)
  324.         *amount = max;
  325.  
  326.     SetCtlValue(control, *amount);
  327.     *amount = value - *amount; /* calculate true change */
  328. }         /* CommonAction */
  329.  
  330. #if !defined(powerc) && !defined(__powerc)
  331. #pragma segment Main
  332. #endif
  333.  
  334. /* Determines how much to change the value of the vertical scrollbar by and how
  335.   much to scroll the TE record. */
  336.  
  337. pascal void VActionProc(ControlHandle control, short part)
  338. {
  339.     short           amount;
  340.     WindowPtr       window;
  341.     DPtr            theDoc;
  342.  
  343.      if (part) {
  344.           window = control[0]->contrlOwner;
  345.         theDoc = DPtrFromWindowPtr(window);
  346.         switch (part) {
  347.         case inUpButton:
  348.         case inDownButton:
  349.             amount = 24;
  350.             break;
  351.  
  352.         case inPageUp:
  353.         case inPageDown:
  354.             amount = (*(theDoc->theText))->viewRect.bottom -
  355.                         (*(theDoc->theText))->viewRect.top;
  356.             break;
  357.         }   /* case */
  358.  
  359.         if (part == inDownButton || part == inPageDown)
  360.              amount = -amount; /* reverse direction */
  361.  
  362.         CommonAction(control, &amount);
  363.  
  364.         if (amount) {
  365.             TEScroll(0, amount, theDoc->theText);
  366.             DrawPageExtras(theDoc);
  367.         }
  368.     }     /* if */
  369. }  /* VActionProc */
  370.  
  371. #if USESROUTINEDESCRIPTORS
  372. RoutineDescriptor    uVActionProc = 
  373.         BUILD_ROUTINE_DESCRIPTOR(uppControlActionProcInfo, VActionProc);
  374. #else
  375. #define uVActionProc *(ControlActionUPP)&VActionProc
  376. #endif
  377.  
  378. #if !defined(powerc) && !defined(__powerc)
  379. #pragma segment Main
  380. #endif
  381.  
  382. /* Determines how much to change the value of the horizontal scrollbar by and how
  383.   much to scroll the TE record. */
  384.  
  385. pascal void HActionProc(ControlHandle control, short part)
  386. {
  387.     short      amount;
  388.     WindowPtr  window;
  389.     DPtr       theDoc;
  390.  
  391.     if (part) {
  392.         window = control[0]->contrlOwner;
  393.         theDoc = DPtrFromWindowPtr(window);
  394.         switch (part) {
  395.         case  inUpButton:
  396.         case  inDownButton:
  397.             amount = kButtonScroll; /* a few pixels */
  398.             break;
  399.         case  inPageUp:
  400.         case  inPageDown:
  401.             amount = (*(theDoc->theText))->viewRect.right -
  402.                         (*(theDoc->theText))->viewRect.left; /* a page */
  403.             break;
  404.         }   /* switch */
  405.         if (part == inDownButton || part == inPageDown)
  406.             amount = - amount; /* reverse direction */
  407.  
  408.         CommonAction(control, &amount);
  409.         if (amount) {
  410.             TEScroll(amount, 0, theDoc->theText);
  411.             DrawPageExtras(theDoc);
  412.         }
  413.     }     /* if */
  414. }         /* HActionProc */
  415.  
  416. #if USESROUTINEDESCRIPTORS
  417. RoutineDescriptor    uHActionProc = 
  418.         BUILD_ROUTINE_DESCRIPTOR(uppControlActionProcInfo, HActionProc);
  419. #else
  420. #define uHActionProc *(ControlActionUPP)&HActionProc
  421. #endif
  422.  
  423. /**-----------------------------------------------------------------------
  424.         Name:         ShowSelect
  425.         Purpose:        Scrolls the text selection into view.
  426.     -----------------------------------------------------------------------**/
  427.  
  428. #if !defined(powerc) && !defined(__powerc)
  429. #pragma segment Window
  430. #endif
  431.  
  432. pascal void ShowSelect(DPtr theDoc)
  433. {
  434.     if (!theDoc)
  435.         return;
  436.         
  437.      AdjustScrollbars(theDoc, false);
  438.  
  439.     /*
  440.         Let TextEdit do the hard work of keeping the selection visible…
  441.     */
  442.  
  443.     TEAutoView(true, theDoc->theText);
  444.     TESelView(theDoc->theText);
  445.     TEAutoView(false, theDoc->theText);
  446.  
  447.     /*
  448.         Now rematch the text and the scrollbars…
  449.     */
  450.  
  451.     SetCtlValue(
  452.         theDoc->hScrollBar,
  453.         (*(theDoc->theText))->viewRect.left -
  454.         (*(theDoc->theText))->destRect.left + kTextOffset);
  455.  
  456.     SetCtlValue(
  457.         theDoc->vScrollBar,
  458.         (*(theDoc->theText))->viewRect.top -
  459.         (*(theDoc->theText))->destRect.top  + kTextOffset);
  460. }  /* ShowSelect */
  461.  
  462. #if !defined(powerc) && !defined(__powerc)
  463. #pragma segment Window
  464. #endif
  465.  
  466. pascal void OffsetWindow(WindowPtr aWindow)
  467. {
  468.     short theWidth;
  469.     short theHeight;
  470.     short theHScreen;
  471.     short theVScreen;
  472.     short xWidth;
  473.     short xHeight;
  474.     short hMax;
  475.     short vMax;
  476.     short wLeft;
  477.     short wTop;
  478.  
  479.     theWidth  = aWindow->portRect.right - aWindow->portRect.left;
  480.     theHeight = aWindow->portRect.bottom - aWindow->portRect.top + kTBarHeight;
  481.  
  482.     theHScreen = qd.screenBits.bounds.right  - qd.screenBits.bounds.left;
  483.     theVScreen = qd.screenBits.bounds.bottom - qd.screenBits.bounds.top;
  484.  
  485.     xWidth  = theHScreen - theWidth;
  486.     xHeight = theVScreen - (theHeight + kMBarHeight);
  487.  
  488.     hMax = (xWidth / kVOffset) + 1;
  489.     vMax = (xHeight / kVOffset) + 1;
  490.  
  491.     gWCount++;
  492.  
  493.     wLeft = (gWCount % hMax) * kVOffset;
  494.     wTop  = ((gWCount % vMax) * kVOffset) + kTBarHeight + kMBarHeight;
  495.  
  496.     MoveWindow(aWindow, wLeft, wTop, false);
  497. }
  498.  
  499.  
  500. /* Returns the update region in local coordinates */
  501.  
  502. pascal void GetLocalUpdateRgn(WindowPtr window, RgnHandle localRgn)
  503. {
  504.     GetWindowUpdateRgn(window, localRgn); /* save old update region */
  505.     OffsetRgn(localRgn, window->portBits.bounds.left, window->portBits.bounds.top); /* convert to local coords */
  506. }          /* GetLocalUpdateRgn */
  507.  
  508. #if !defined(powerc) && !defined(__powerc)
  509. #pragma segment Window
  510. #endif
  511.  
  512. pascal void IssueZoomCommand(WindowPtr whichWindow, short whichPart);
  513. pascal void IssueSizeWindow(WindowPtr whichWindow,short newHSize, short newVSize);
  514.  
  515. pascal void MyGrowWindow(WindowPtr w, Point p)
  516. {
  517.     GrafPtr savePort;
  518.     long    theResult;
  519.     Rect    r;
  520.  
  521.     GetPort(&savePort);
  522.     SetPort(w);
  523.     SetRect(&r, 80, 80, qd.screenBits.bounds.right, qd.screenBits.bounds.bottom);
  524.     theResult = GrowWindow(w, p, &r);
  525.     if (theResult)
  526.         IssueSizeWindow(w, LoWord(theResult), HiWord(theResult));
  527.  
  528.     SetPort(savePort);
  529. }
  530.  
  531. #if !defined(powerc) && !defined(__powerc)
  532. #pragma segment Window
  533. #endif
  534.  
  535. pascal void DoZoom(WindowPtr w, short c, Point     p)
  536. {
  537.     GrafPtr savePort;
  538.  
  539.      GetPort(&savePort);
  540.     SetPort(w);
  541.      if (TrackBox(w, p, c)) {
  542.         EraseRect(&w->portRect);
  543.         IssueZoomCommand(w, c);
  544.     }
  545. }
  546.  
  547. #if !defined(powerc) && !defined(__powerc)
  548. #pragma segment Window
  549. #endif
  550.  
  551. pascal void DoThumb(DPtr theDoc, ControlHandle cntl, short oldValue)
  552. {
  553.     short value = oldValue - GetCtlValue(cntl);
  554.     if (value) {
  555.         if (cntl == theDoc->vScrollBar)
  556.             TEScroll(0, value, theDoc->theText);
  557.         else
  558.             TEScroll(value, 0, theDoc->theText);
  559.         DrawPageExtras(theDoc);
  560.     }
  561. }
  562.  
  563. #if !defined(powerc) && !defined(__powerc)
  564. #pragma segment Window
  565. #endif
  566.  
  567. pascal void DoContent(WindowPtr theWindow, EventRecord * theEvent)
  568. {
  569.     short         cntlCode;
  570.     short         part;
  571.     ControlHandle theControl;
  572.     GrafPtr       savePort;
  573.     Boolean       extend;
  574.     DPtr          theDoc;
  575.     short         value;
  576.  
  577.     GetPort(&savePort);
  578.     SetPort(theWindow);
  579.     theDoc = DPtrFromWindowPtr(theWindow);
  580.  
  581.     GlobalToLocal(&theEvent->where);
  582.     cntlCode = FindControl(theEvent->where, theWindow, &theControl);
  583.  
  584.     /*only extend the selection if the shiftkey is down*/
  585.     if (cntlCode == 0) {
  586.           extend = (theEvent->modifiers & shiftKey) != 0;
  587.  
  588.         if (PtInRect(theEvent->where, &(*(theDoc->theText))->viewRect)) {
  589.             TEClick(theEvent->where, extend, theDoc->theText);
  590.             if (theEvent->modifiers & cmdKey)
  591.                 Explain(theDoc);
  592.         }
  593.     } else if (cntlCode == inThumb) {
  594.         value = GetCtlValue(theControl);
  595.         if (TrackControl(theControl, theEvent->where, nil))
  596.             DoThumb(theDoc, theControl, value);
  597.     } else
  598.         if (theControl == theDoc->vScrollBar)
  599.             part = TrackControl(theControl, theEvent->where, &uVActionProc);
  600.         else
  601.             part = TrackControl(theControl, theEvent->where, &uHActionProc);
  602.  
  603.     SetPort(savePort);
  604. }
  605.  
  606. #if !defined(powerc) && !defined(__powerc)
  607. #pragma segment Window
  608. #endif
  609.  
  610. pascal void AdjustScript(DPtr doc)
  611. {
  612.     ScriptCode    fontScript;
  613.     ScriptCode    keyScript;
  614.  
  615.     fontScript = FontToScript(doc->theText[0]->txFont);
  616.     keyScript  = GetScriptManagerVariable(smKeyScript);
  617.     
  618.     /* White man's burden: A roman keyboard script fits everywhere. */
  619.     if (keyScript != fontScript && keyScript != smRoman) 
  620.         KeyScript(fontScript);
  621. }
  622.  
  623. #if !defined(powerc) && !defined(__powerc)
  624. #pragma segment Window
  625. #endif
  626.  
  627. pascal OSErr DoActivate(WindowPtr theWindow, Boolean   activate)
  628. {
  629.     OSErr err;
  630.     Rect  r;
  631.     DPtr  theDoc;
  632.  
  633.     err = noErr;
  634.  
  635.     if (theWindow) {
  636.         theDoc = DPtrFromWindowPtr(theWindow);
  637.         SetPort(theWindow);
  638.         DrawGrowIcon(theWindow);
  639.         GetWinContentRect(theWindow, &r);
  640.         InvalRect(&r);
  641.         if (activate) {
  642.             gActiveWindow = theWindow;
  643.             
  644.             TEActivate(theDoc->theText);
  645.             ShowControl(theDoc->vScrollBar);
  646.             ShowControl(theDoc->hScrollBar);
  647.             DisableItem(myMenus[editM], undoCommand);
  648.             AdjustScript(theDoc);
  649.             if (theDoc->tsmDoc)
  650.                 ActivateTSMDocument(theDoc->tsmDoc);
  651.         } else {
  652.             if (theDoc->tsmDoc)
  653.                 DeactivateTSMDocument(theDoc->tsmDoc);
  654.  
  655.             gActiveWindow = nil;
  656.             
  657.             TEDeactivate(theDoc->theText);
  658.             HideControl(theDoc->vScrollBar);
  659.             HideControl(theDoc->hScrollBar);
  660.         }
  661.     }
  662.  
  663.       return err;
  664. }
  665.  
  666. #if !defined(powerc) && !defined(__powerc)
  667. #pragma segment Window
  668. #endif
  669.  
  670. pascal void GetPageEnds(
  671.     short          pageHeight,
  672.     TEHandle       theText,
  673.     PageEndsArray  pageBounds,
  674.     short          *nPages)
  675. {
  676.     short  pageBase;      /* total pixel offset of pages so far */
  677.     short  thisLine;
  678.     short  lastLine;
  679.     short  pageSoFar;
  680.     short  thisPage;      /* Current page being calced */
  681.     short  thisLineH;     /* Height of text line */
  682.     short  pageFirstLine; /* Line # of top of page */
  683.  
  684.     pageBase   = 0;
  685.     thisLine   = 1;
  686.     lastLine   = theText[0]->nLines;
  687.  
  688.     thisPage   = 0;
  689.     pageSoFar  = 0;
  690.     while ((thisLine <= lastLine) || (pageSoFar!=0)) {
  691.         pageFirstLine = thisLine;
  692.         thisLineH     = TEGetHeight(thisLine, thisLine, theText);
  693.  
  694.         while ((thisLineH+pageSoFar<pageHeight) && (thisLine <= lastLine)) {
  695.             pageSoFar += thisLineH;
  696.             thisLine++;
  697.             thisLineH = TEGetHeight(thisLine, thisLine, theText);
  698.         }
  699.  
  700.         if (pageSoFar) {
  701.             pageBounds[thisPage] = pageSoFar+pageBase;
  702.             pageBase  = pageBounds[thisPage];
  703.             thisPage++;
  704.             pageSoFar = 0;
  705.         }
  706.  
  707.         /*
  708.             Special case text line taller than page
  709.         */
  710.  
  711.         if ((thisLine  == pageFirstLine) && (thisLineH > pageHeight)) {
  712.             do {
  713.                 pageBounds[thisPage] = pageBase+pageHeight;
  714.                 pageBase   = pageBounds[thisPage];
  715.                 thisPage  += 1;
  716.                 thisLineH -= pageHeight;
  717.             } while (thisLineH >= pageHeight);
  718.             pageSoFar = thisLineH; /* Carry bottom of large line to next page */
  719.             thisLine += 1; /* carry xs on as pageSoFar and start measuring next line */
  720.         }
  721.     }
  722.  
  723.     *nPages = thisPage;
  724. }  /* GetPageEnds */
  725.  
  726. pascal void DrawPageBreaks(DPtr theDoc)
  727. {
  728.     PageEndsArray    pageEnds;
  729.     short           nPages;
  730.     short           ctr;
  731.     short           lineBase;
  732.     short           pageHeight;
  733.     Rect            viewRect;
  734.  
  735.     pageHeight = theDoc->pageSize.bottom - theDoc->pageSize.top;
  736.  
  737.     GetPageEnds(pageHeight, theDoc->theText, pageEnds, &nPages);
  738.  
  739.     lineBase = (*(theDoc->theText))->destRect.top;
  740.     viewRect = (*(theDoc->theText))->viewRect;
  741.  
  742.     PenPat(&qd.gray);
  743.     for (ctr = 0; ctr<nPages-1; ctr++) {
  744.         MoveTo(viewRect.left, lineBase+pageEnds[ctr]);
  745.         LineTo(viewRect.right,lineBase+pageEnds[ctr]);
  746.     }
  747.     PenNormal();
  748. } /*    DrawPageBreaks */
  749.  
  750. pascal void DrawPageExtras(DPtr theDoc)
  751. {
  752.     GrafPtr       oldPort;
  753.     RgnHandle    oldClip;
  754.     Rect              rectToClip;
  755.  
  756.     GetPort(&oldPort);
  757.     SetPort(theDoc->theWindow);
  758.  
  759.     oldClip = NewRgn();
  760.     GetClip(oldClip);
  761.  
  762.     GetWinContentRect(theDoc->theWindow,&rectToClip);
  763.     ClipRect(&rectToClip);
  764.  
  765. #ifdef EVIL_USELESS_EDITIONS
  766.     /* draw the borders */
  767.  
  768.     if (theDoc->kind == kDocumentWindow && theDoc->u.reg.showBorders)
  769.         ShowSectionBorders(theDoc);
  770. #endif
  771.  
  772.     /* and then the page breaks */
  773.     /* DrawPageBreaks(theDoc);  Take the page breaks and shove 'em MN */
  774.  
  775.     SetClip(oldClip);
  776.  
  777.     DisposeRgn(oldClip);
  778.  
  779.     SetPort(oldPort);
  780. }  /* DrawPageExtras */
  781.  
  782. #define PlotResMiniIcon(id, r)    PlotIconID(r, atNone, ttNone, id)
  783.  
  784. pascal void DoUpdate(DPtr theDoc, WindowPtr theWindow)
  785. {
  786.     GrafPtr       savePort;
  787.     Rect             rectClip;
  788.     Rect             r;
  789.     short            icmVBase;
  790.  
  791.     GetPort(&savePort);
  792.     SetPort(theWindow);
  793.     BeginUpdate(theWindow);
  794.  
  795.     ClipRect(&theWindow->portRect);
  796.     EraseRect(&theWindow->portRect);
  797.     
  798.     if (theDoc) {
  799.         icmVBase = theWindow->portRect.bottom - 13;
  800.         
  801.         SetRect(&r, 2, icmVBase, 18, icmVBase+12);
  802.         switch (theDoc->lastState & 0x000F) {
  803.         case stateConsole:
  804.             PlotResMiniIcon(ConsoleSICNID, &r);
  805.             break;
  806.         case stateDocument:
  807.             PlotResMiniIcon(DocumentSICNID, &r);
  808.             break;
  809.         }
  810.         SetRect(&r, 18, icmVBase, 34, icmVBase+12);
  811.         switch (theDoc->lastState & 0x00F0) {
  812.         case stateRdWr:
  813.             PlotResMiniIcon(EnabledSICNID, &r);
  814.             break;
  815.         case stateRdOnly:
  816.             PlotResMiniIcon(ReadOnlySICNID, &r);
  817.             break;
  818.         case stateBlocked:
  819.             PlotResMiniIcon(BlockedSICNID, &r);
  820.             break;
  821.         }
  822.         
  823.         DrawControls(theWindow);
  824.         DrawGrowIcon(theWindow);
  825.     
  826.         GetWinContentRect(theWindow, &rectClip);
  827.         ClipRect(&rectClip);
  828.     
  829.         TEUpdate(&theWindow->portRect, theDoc->theText);
  830.     
  831.         DrawPageExtras(theDoc);
  832.     }
  833.     
  834.     EndUpdate(theWindow);
  835.     ClipRect(&theWindow->portRect);
  836.  
  837.     SetPort(savePort);
  838. } /* DoUpdate */
  839.  
  840. #if !defined(powerc) && !defined(__powerc)
  841. #pragma segment Window
  842. #endif
  843.  
  844. pascal DPtr NewDocument(Boolean isForOldDoc, WindowKind kind)
  845. {
  846.     short                resFile;
  847.     Rect           destRect;
  848.     Rect           viewRect;
  849.     Rect           vScrollRect;
  850.     Rect           hScrollRect;
  851.     DPtr           myDoc;
  852.     WindowPtr      myWindow;
  853.     ControlHandle  vScroll;
  854.     ControlHandle  hScroll;
  855.     Str255         theName;
  856.     Str255         newNumber;
  857.     OSType            supportedInterfaces[1];
  858.  
  859.     myDoc = nil;
  860.     myWindow = GetNewWindow(WindowTemplates+kind, nil, (WindowPtr)-1);
  861.  
  862.     if (!myWindow)
  863.         return nil;
  864.  
  865.     if (!isForOldDoc && kind == kDocumentWindow) {
  866.         GetWTitle(myWindow, theName);
  867.         NumToString(++gNewDocCount, newNumber);
  868.         if (gNewDocCount>1) {
  869.             PLstrcat(theName, (StringPtr) "\p #");
  870.             PLstrcat(theName, newNumber);
  871.             SetWTitle(myWindow, theName);
  872.         }
  873.     }
  874.  
  875.     OffsetWindow(myWindow);
  876.     SetPort(myWindow);
  877.  
  878.     myDoc = (DPtr)NewPtr(sizeof(DocRec));
  879.  
  880.     SetWRefCon(myWindow, (long)myDoc);
  881.     SetWindowKind(myWindow, PerlWindowKind);
  882.  
  883.     myDoc->theWindow = myWindow;
  884.  
  885.     vScrollRect = myWindow->portRect;
  886.  
  887.     vScrollRect.left  = vScrollRect.right - kScrollbarAdjust;
  888.     vScrollRect.right = vScrollRect.left  + kScrollbarWidth;
  889.  
  890.     vScrollRect.bottom = vScrollRect.bottom - 14;
  891.     vScrollRect.top    = vScrollRect.top - 1;
  892.  
  893.     vScroll = NewControl(myWindow, &vScrollRect, (StringPtr) "\p", true, 0, 0, 0, scrollBarProc, 0);
  894.  
  895.     hScrollRect = myWindow->portRect;
  896.     hScrollRect.top = hScrollRect.bottom - kScrollbarAdjust;
  897.     hScrollRect.bottom = hScrollRect.top + kScrollbarWidth;
  898.  
  899.     hScrollRect.right = hScrollRect.right - 14;
  900.     hScrollRect.left  = hScrollRect.left + 31;
  901.     hScroll = NewControl(myWindow, &hScrollRect, (StringPtr) "\p", true, 0, 0, 0, scrollBarProc, 0);
  902.  
  903.     myDoc->vScrollBar = vScroll;
  904.     myDoc->hScrollBar = hScroll;
  905.     myDoc->type            = kPlainTextDoc;
  906.     myDoc->kind         = kind;
  907.     myDoc->dirty         = false;
  908.  
  909.     if (kind == kDocumentWindow) {
  910. #ifdef EVIL_USELESS_EDITIONS
  911.         myDoc->u.reg.lastID            = 0;
  912.         myDoc->u.reg.firstSection    = nil;
  913.         myDoc->u.reg.lastSection    = nil;
  914.         myDoc->u.reg.numSections    = 0;
  915. #endif
  916.         myDoc->u.reg.everSaved     = false;
  917.         myDoc->u.reg.everLoaded    = false;
  918.         myDoc->u.reg.showBorders     = false;
  919.         
  920.         myDoc->lastState                = stateDocument + stateRdWr;
  921.     } else {
  922.         myDoc->u.cons.next            = gConsoleList;
  923.         myDoc->u.cons.cookie            = nil;
  924.         myDoc->u.cons.fence            = 0;
  925.         myDoc->u.cons.memory            = 20000;
  926.         myDoc->u.cons.selected        = false;
  927.  
  928.         gConsoleList = myDoc;
  929.  
  930.         myDoc->lastState                = stateConsole + stateBlocked;
  931.     }
  932.  
  933.     GetTERect(myWindow, &viewRect);
  934.     destRect = viewRect;
  935.  
  936.     myDoc->thePrintSetup = (THPrint)NewHandle(sizeof(TPrint));
  937.  
  938.     resFile = CurResFile();
  939.     
  940.     PrOpen();
  941.     PrintDefault(myDoc->thePrintSetup);
  942.     PrClose();
  943.     
  944.     UseResFile(resFile);
  945.  
  946.     myDoc->pageSize = (*(myDoc->thePrintSetup))->prInfo.rPage;
  947.     OffsetRect(&myDoc->pageSize, -myDoc->pageSize.left, -myDoc->pageSize.top);
  948.  
  949.     destRect.right = destRect.left + myDoc->pageSize.right;
  950.  
  951.     OffsetRect(&destRect, kTextOffset, kTextOffset);
  952.  
  953.     TextFont(gFormat.font);
  954.     TextSize(gFormat.size);
  955.     TextFace(0);
  956.  
  957.     myDoc->theText = TENew(&destRect, &viewRect);
  958.     
  959.     myDoc->theText[0]->crOnly = -1;
  960.     myDoc->theFileName[0] = 0;
  961.     myDoc->theWindow      = myWindow;
  962.  
  963.     myDoc->tsmDoc                =    nil;
  964.     myDoc->tsmTERecHandle    =    nil;
  965.     
  966.     if (gTSMTEImplemented) {
  967.         supportedInterfaces[0] = kTSMTEInterfaceType;
  968.         if (NewTSMDocument(1, supportedInterfaces, &myDoc->tsmDoc,
  969.                     (long) &myDoc->tsmTERecHandle) == noErr)
  970.         {
  971.             TSMTERecPtr tsmteRecPtr = *(myDoc->tsmTERecHandle);
  972.             
  973.             tsmteRecPtr->textH = myDoc->theText;
  974.             tsmteRecPtr->preUpdateProc = nil;
  975.             tsmteRecPtr->postUpdateProc = nil;
  976.             tsmteRecPtr->updateFlag = 0;
  977.             tsmteRecPtr->refCon = (long) myDoc->theWindow;
  978.             
  979.             UseInputWindow(myDoc->tsmDoc, !gPerlPrefs.inlineInput);
  980.         } else {
  981.             myDoc->tsmDoc                =    nil;
  982.             myDoc->tsmTERecHandle    =    nil;
  983.         }
  984.     }
  985.         
  986.     ResizeWindow(myDoc);
  987.     
  988.     RegisterDocument(myDoc);
  989.     
  990.     return(myDoc);
  991. }
  992.  
  993. #if !defined(powerc) && !defined(__powerc)
  994. #pragma segment Window
  995. #endif
  996.  
  997. pascal void CloseMyWindow(WindowPtr aWindow)
  998. {
  999.     DPtr     aDocument;
  1000.     TEHandle theText;
  1001.  
  1002.     DoHideWindow(aWindow);
  1003.     aDocument = DPtrFromWindowPtr(aWindow);
  1004.  
  1005.     UnregisterDocument(aDocument);
  1006.  
  1007.     if (aDocument->tsmDoc) {
  1008.         FixTSMDocument(aDocument->tsmDoc);
  1009.         // DeleteTSMDocument might cause crash if we don't deactivate first, so...
  1010.         DeactivateTSMDocument(aDocument->tsmDoc);
  1011.         DeleteTSMDocument(aDocument->tsmDoc);
  1012.     }
  1013.  
  1014.     if (aDocument->kind != kDocumentWindow) {
  1015.         CloseConsole(aDocument->u.cons.cookie);
  1016.         
  1017.         if (gConsoleList == aDocument)
  1018.             gConsoleList = aDocument->u.cons.next;
  1019.         else {
  1020.             DPtr doc = gConsoleList;
  1021.             while (doc->u.cons.next != aDocument)
  1022.                 doc = doc->u.cons.next;
  1023.             doc->u.cons.next = aDocument->u.cons.next;
  1024.         }
  1025.     }
  1026.     
  1027.     theText   = aDocument->theText;
  1028.     TEDispose(theText);
  1029.  
  1030.     if (aDocument->thePrintSetup)
  1031.         DisposHandle((Handle)aDocument->thePrintSetup);
  1032.  
  1033.     DisposPtr((Ptr)aDocument);
  1034.     DisposeWindow(aWindow);
  1035.  
  1036.     gWCount--;
  1037. }
  1038.  
  1039. /*
  1040.     Name     : PrintWindow
  1041.     Function : Prints the document supplied in theDoc. askUser controls interaction
  1042.                   with the user.
  1043.  
  1044.                          Uses extra memory equal to the size of the textedit use in the
  1045.                          printed document.
  1046. */
  1047.  
  1048. pascal void PrintWindow(DPtr theDoc, Boolean askUser)
  1049. {
  1050.     GrafPtr          oldPort;
  1051.     TEHandle         printerTE;
  1052.     TPPrPort         printerPort;
  1053.     Rect                 printView;
  1054.     PageEndsArray    pageBounds;
  1055.     short              nPages;
  1056.     short               pageCtr;
  1057.     Boolean             abort;
  1058.     short                resFile;
  1059.     Rect                 rectToClip;
  1060.     TPrStatus         thePrinterStatus;
  1061.     DialogPtr         progressDialog;
  1062.  
  1063.     abort = false;
  1064.  
  1065.     /*
  1066.         Preserve the current port
  1067.     */
  1068.     GetPort(&oldPort);
  1069.     resFile = CurResFile();
  1070.     PrOpen();
  1071.  
  1072.     if (askUser)
  1073.         if (abort = !PrJobDialog(theDoc->thePrintSetup)) {
  1074.             PrClose();
  1075.             
  1076.             goto done;
  1077.         }
  1078.  
  1079.     progressDialog = GetNewDialog(1005, nil, (WindowPtr)-1);
  1080.  
  1081.     DrawDialog(progressDialog);
  1082.  
  1083.     printerPort = PrOpenDoc(theDoc->thePrintSetup, nil, nil);
  1084.     SetPort((GrafPtr)printerPort);
  1085.  
  1086.     /*
  1087.         Put the window text into the printer port
  1088.     */
  1089.     TextFont(theDoc->theText[0]->txFont);
  1090.     TextSize(theDoc->theText[0]->txSize);
  1091.  
  1092.     printView = (*(theDoc->thePrintSetup))->prInfo.rPage;
  1093.     printerTE = TENew(&printView, &printView);
  1094.  
  1095.     HLock((Handle)((*(theDoc->theText))->hText));
  1096.  
  1097.     TESetText(*((*(theDoc->theText))->hText), (*(theDoc->theText))->teLength, printerTE);
  1098.  
  1099.     HUnlock((Handle)((*(theDoc->theText))->hText));
  1100.  
  1101.     /*
  1102.         Work out the offsets
  1103.     */
  1104.     printerTE[0]->destRect = printView; /* GetPageEnds calls TECalText */
  1105.  
  1106.     GetPageEnds(printView.bottom-printView.top, printerTE, pageBounds, &nPages);
  1107.  
  1108.     TEDeactivate(printerTE);
  1109.  
  1110.     for (pageCtr = 0; pageCtr <= nPages-1; pageCtr++)
  1111.         if (!abort) {
  1112.             PrOpenPage(printerPort, nil);
  1113.  
  1114.             rectToClip = printView;
  1115.  
  1116.             if (pageCtr > 0)
  1117.                 rectToClip.bottom = rectToClip.top + (pageBounds[pageCtr]-pageBounds[pageCtr-1]);
  1118.             else
  1119.                 rectToClip.bottom = rectToClip.top + pageBounds[pageCtr];
  1120.  
  1121.             ClipRect(&rectToClip);
  1122.  
  1123.             if (PrError() == iPrAbort)
  1124.                 abort = true;
  1125.  
  1126.             if (! abort)
  1127.                 TEUpdate(&printView, printerTE);
  1128.  
  1129.             if (PrError() == iPrAbort)
  1130.                 abort = true;
  1131.  
  1132.             PrClosePage(printerPort);
  1133.  
  1134.             TEScroll(0,rectToClip.top-rectToClip.bottom, printerTE);
  1135.         }
  1136.  
  1137.     TEDispose(printerTE);
  1138.     PrCloseDoc(printerPort);
  1139.  
  1140.     if (( (*(theDoc->thePrintSetup))->prJob.bJDocLoop == bSpoolLoop ) &&
  1141.             ( PrError() == noErr )  &&
  1142.             (! abort))
  1143.         PrPicFile( theDoc->thePrintSetup, nil, nil, nil, &thePrinterStatus);
  1144.  
  1145.     PrClose();
  1146.  
  1147.     DisposDialog(progressDialog);
  1148.  
  1149. done:
  1150.     SetPort(oldPort);
  1151.     UseResFile(resFile);
  1152.     InvalRect(&oldPort->portRect);
  1153. }
  1154.  
  1155. void ForceStatusRedraw(WindowPtr win)
  1156. {
  1157.     GrafPtr    oldPort;
  1158.     Rect        r;
  1159.     
  1160.     GetPort(&oldPort);
  1161.     SetPort(win);
  1162.  
  1163.     r = win->portRect;
  1164.     
  1165.     r.right = 30;
  1166.     r.top   = r.bottom - 13;
  1167.     
  1168.     InvalRect(&r);
  1169.     
  1170.     SetPort(oldPort);
  1171. }
  1172.  
  1173. pascal void ShowWindowStatus()
  1174. {
  1175.      DPtr           aDocument;
  1176.     WindowPtr    win;
  1177.     short            curState;
  1178.  
  1179.     for (win = FrontWindow(); win; win = GetNextWindow(win)) {
  1180.         if (Ours(win)) {
  1181.             aDocument = DPtrFromWindowPtr(win);
  1182.  
  1183.             if (aDocument->kind == kDocumentWindow) 
  1184.                 curState = stateDocument + stateRdWr;
  1185.             else {
  1186.                 curState = stateConsole;
  1187.                 if (aDocument->u.cons.fence == 32767)
  1188.                     curState    += stateRdOnly;
  1189.                 else if (!gRunningPerl || !aDocument->u.cons.selected)
  1190.                     curState += stateBlocked;
  1191.                 else
  1192.                     curState += stateRdWr;
  1193.             }
  1194.             
  1195.             if (curState != aDocument->lastState) {
  1196.                 aDocument->lastState = curState;
  1197.                 ForceStatusRedraw(win);
  1198.             }
  1199.         }
  1200.     }
  1201. }
  1202.  
  1203. pascal void UseInlineInput(Boolean doInline)
  1204. {
  1205.      DPtr           aDocument;
  1206.     WindowPtr    win;
  1207.  
  1208.     for (win = FrontWindow(); win; win = GetNextWindow(win)) {
  1209.         if (Ours(win)) {
  1210.             aDocument = DPtrFromWindowPtr(win);
  1211.             if (aDocument->tsmDoc)
  1212.                 UseInputWindow(aDocument->tsmDoc, !doInline);
  1213.         }
  1214.     }
  1215. }
  1216.  
  1217. pascal void DoShowWindow(WindowPtr win)
  1218. {
  1219.     ShowWindow(win);
  1220.  
  1221.     for (win = FrontWindow(); win; win = GetNextWindow(win))
  1222.         if (IsWindowVisible(win) && Ours(win)) {
  1223.             SetLongMenus();
  1224.             
  1225.             return;
  1226.         }
  1227. }
  1228.  
  1229. pascal void DoHideWindow(WindowPtr win)
  1230. {
  1231.     HideWindow(win);
  1232.  
  1233.     for (win = FrontWindow(); win; win = GetNextWindow(win))
  1234.         if (IsWindowVisible(win) && Ours(win))
  1235.             return;
  1236.     
  1237.     SetShortMenus();
  1238. }
  1239.  
  1240. pascal WindowPtr AlreadyOpen(FSSpec * spec, StringPtr name)
  1241. {
  1242.      DPtr           aDocument;
  1243.     WindowPtr    win;
  1244.     Str255        title;
  1245.  
  1246.     for (win = FrontWindow(); win; win = GetNextWindow(win))
  1247.         if (Ours(win)) {
  1248.             aDocument = DPtrFromWindowPtr(win);
  1249.             if (aDocument->kind == kDocumentWindow && aDocument->u.reg.everSaved) {
  1250.                 if (SameFSSpec(spec, &aDocument->theFSSpec))
  1251.                     return win;
  1252.             } else if (name) {
  1253.                 GetWTitle(win, title);
  1254.                 if (EqualString(title, name, false, true))
  1255.                     return win;
  1256.             }
  1257.         }
  1258.     
  1259.     return nil;
  1260. }
  1261.